<?php
/**
 * 视图控制器
 */
namespace app\manage\controller\bonus;

use app\manage\controller\Base;

class Api extends Base
{
	/**
	 * 查询所有奖金
	 * @return [type] [description]
	 */
	public function bonus(){
		$data = model('bonus')::bonusData();
		$data = array_values($data);
		return json($data);
	}
	/**
	 * 奖金明细记录
	 * @return [type] [description]
	 */
	public function record(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }

        $data = model('bonus_record')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);

	}
	/**
	 * 查询插件的某类配置项
	 * @return [type] [description]
	 */
	public function bonus_config(){
		$params = $this->request->param();

		$bonus_name = $params['bonus'];
		$config_name = $params['config'];

		$config = model('bonus_config')::bonusConfig($bonus_name,$config_name);
		$config['config_value'] = unserialize($config['config_value']);

		return json($config);

	}

	
	/**
	 * 计算奖金
	 * @return [type] [description]
	 */
	public function deal_bonus(){

		set_time_limit(0);
		ignore_user_abort(true); 

		$params = $this->request->param();
		$bonus_name = isset($params['bonus_name'])?$params['bonus_name']:'';
		$operate_id = isset($params['operate_id'])?$params['operate_id']:null;
		if(!$bonus_name){
			$bonus = ['lingshou','xiaoshou','fuwu','score'];
			//$bonus = ['xiaoshou'];
			foreach($bonus as $value):
				$bonus_action = new \app\common\bonus\Action($value,$operate_id);
				$bonus_action -> deal();
			endforeach;
			$this->success('OK');
		}else{
			$bonus_action = new \app\common\bonus\Action($bonus_name,$operate_id);
			if(true===$bonus_action -> deal()){
				$this->success('OK');
			}else{
				$this->error('FAIL');
			}
		}
		
	}

	public function deal_world(){
		set_time_limit(0);
		ignore_user_abort(true);

		$bonus = model('bonus');
		if(false===$bonus->world()){
			$this->error($bonus->getError());
		}else{
			$this->success('OK');
		}
	}

	public function deal_jiaquan(){
		set_time_limit(0);
		ignore_user_abort(true);

		$bonus = model('bonus');
		if(false===$bonus->jiaquan()){
			$this->error($bonus->getError());
		}else{
			$this->success('OK');
		}
	}

}